// pages/exhibition/exhibition.js
Page({
  data: {
    showIntroModal: false, // 控制展馆介绍弹窗显示
    sessionList: [
      {
        id: 1,
        title: '生命教育主题展',
        date: '2025.08.20',
        time: '09:00-11:00',
        location: '生命教育展馆A厅',
        totalSeats: 50,
        remainingSeats: 15,
        status: 'available',
        description: '通过多媒体展示和互动体验，深入了解生命的意义和价值。',
        content: '展馆将展示遗体捐献者的感人故事，通过图片、视频、实物等多种形式，让参观者感受捐献者的大爱精神。'
      },
      {
        id: 2,
        title: '捐献者故事分享会',
        date: '2025.08.22',
        time: '14:00-16:00',
        location: '生命教育展馆B厅',
        totalSeats: 30,
        remainingSeats: 0,
        status: 'full',
        description: '邀请捐献者家属分享感人故事，传递生命教育理念。',
        content: '通过捐献者家属的亲身讲述，了解捐献背后的感人故事，感受生命的延续和爱的传递。'
      },
      {
        id: 3,
        title: '医学知识科普展',
        date: '2025.08.25',
        time: '10:00-12:00',
        location: '生命教育展馆C厅',
        totalSeats: 40,
        remainingSeats: 25,
        status: 'available',
        description: '普及医学知识，了解人体器官捐献的重要性。',
        content: '通过专业的医学知识展示，让参观者了解器官捐献的科学原理和社会意义。'
      }
    ],
    myReservations: [
      {
        id: 1,
        sessionId: 1,
        sessionTitle: '生命教育主题展',
        sessionDate: '2025.08.20',
        sessionTime: '09:00-11:00',
        reservationDate: '2025.08.15',
        reservationTime: '14:30',
        reservationCode: 'EX20250820001',
        status: 'confirmed',
        statusText: '已确认'
      }
    ]
  },

  onLoad(options) {
    console.log('生命教育展馆页面加载');
    this.loadData();
    // 页面加载时自动显示展馆介绍弹窗
    this.setData({
      showIntroModal: true
    });
  },

  onShow() {
    // 页面显示时刷新数据
    this.loadData();
  },

  // 加载数据
  loadData() {
    // 这里可以从服务器获取最新数据
    console.log('加载展馆数据');
  },

  // 查看场次信息
  viewSessions() {
    wx.navigateTo({
      url: '/pages/exhibition/sessions/sessions'
    });
  },

  // 查看所有场次
  viewAllSessions() {
    wx.navigateTo({
      url: '/pages/exhibition/sessions/sessions'
    });
  },

  // 查看场次详情
  viewSessionDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/exhibition/session-detail/session-detail?id=${id}`
    });
  },

  // 预约场次
  makeReservation(e) {
    const id = e.currentTarget.dataset.id;
    const session = this.data.sessionList.find(item => item.id == id);
    
    if (!session) {
      wx.showToast({
        title: '场次信息不存在',
        icon: 'none'
      });
      return;
    }

    if (session.status !== 'available') {
      wx.showToast({
        title: '该场次已满员',
        icon: 'none'
      });
      return;
    }

    // 跳转到预约页面
    wx.navigateTo({
      url: `/pages/exhibition/reservation/reservation?sessionId=${id}`
    });
  },

  // 签到功能
  checkIn(e) {
    const id = e.currentTarget.dataset.id;
    const reservation = this.data.myReservations.find(item => item.id == id);
    
    if (!reservation) {
      wx.showToast({
        title: '预约信息不存在',
        icon: 'none'
      });
      return;
    }

    if (reservation.status !== 'confirmed') {
      wx.showToast({
        title: '预约状态异常',
        icon: 'none'
      });
      return;
    }

    // 调用扫码签到
    wx.scanCode({
      success: (res) => {
        console.log('扫码结果:', res);
        this.processCheckIn(reservation, res.result);
      },
      fail: (err) => {
        console.log('扫码失败:', err);
        wx.showToast({
          title: '扫码失败',
          icon: 'none'
        });
      }
    });
  },

  // 处理签到
  processCheckIn(reservation, scanResult) {
    wx.showLoading({
      title: '签到中...'
    });

    // 模拟签到处理
    setTimeout(() => {
      wx.hideLoading();
      
      // 这里应该验证扫码结果是否匹配预约码
      if (scanResult === reservation.reservationCode) {
        // 更新预约状态
        const updatedReservations = this.data.myReservations.map(item => {
          if (item.id === reservation.id) {
            return {
              ...item,
              status: 'checked_in',
              statusText: '已签到'
            };
          }
          return item;
        });

        this.setData({
          myReservations: updatedReservations
        });

        wx.showToast({
          title: '签到成功',
          icon: 'success'
        });
      } else {
        wx.showToast({
          title: '预约码不匹配',
          icon: 'none'
        });
      }
    }, 1500);
  },

  // 查看预约状态
  checkReservation() {
    wx.navigateTo({
      url: '/pages/exhibition/my-reservations/my-reservations'
    });
  },

  // 签到功能（从主页直接调用）
  checkIn() {
    wx.navigateTo({
      url: '/pages/exhibition/check-in/check-in'
    });
  },

  // 取消预约
  cancelReservation(e) {
    const id = e.currentTarget.dataset.id;
    const reservation = this.data.myReservations.find(item => item.id == id);
    
    if (!reservation) {
      wx.showToast({
        title: '预约信息不存在',
        icon: 'none'
      });
      return;
    }

    wx.showModal({
      title: '确认取消',
      content: `确定要取消预约"${reservation.sessionTitle}"吗？`,
      success: (res) => {
        if (res.confirm) {
          this.processCancelReservation(reservation);
        }
      }
    });
  },

  // 处理取消预约
  processCancelReservation(reservation) {
    wx.showLoading({
      title: '取消中...'
    });

    setTimeout(() => {
      wx.hideLoading();
      
      // 从预约列表中移除
      const updatedReservations = this.data.myReservations.filter(item => item.id !== reservation.id);
      
      this.setData({
        myReservations: updatedReservations
      });

      wx.showToast({
        title: '取消成功',
        icon: 'success'
      });
    }, 1000);
  },

  // 分享功能
  onShareAppMessage() {
    return {
      title: '生命教育展馆 - 传承大爱精神',
      path: '/pages/exhibition/exhibition',
      imageUrl: '/images/share-exhibition.png'
    };
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: '生命教育展馆 - 传承大爱精神',
      imageUrl: '/images/share-exhibition.png'
    };
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.loadData();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  },

  // 显示展馆介绍弹窗
  showIntroModal() {
    this.setData({
      showIntroModal: true
    });
  },

  // 隐藏展馆介绍弹窗
  hideIntroModal() {
    this.setData({
      showIntroModal: false
    });
  },

  // 进入展馆
  enterExhibition() {
    this.hideIntroModal();
    wx.showToast({
      title: '欢迎参观展馆',
      icon: 'success',
      duration: 2000
    });
    // 这里可以跳转到展馆详情页面或执行其他操作
  }
});